package com.alkaid.anime.controller;

import com.alkaid.anime.service.HistoryService;
import com.alkaid.commons.controller.BaseController;
import com.alkaid.commons.param.anime.AnimeHistoryParam;
import com.alkaid.commons.utils.JsonResult;
import com.alkaid.commons.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Kylin
 */
@RestController
@RequestMapping("api/anime")
public class HistoryController extends BaseController {

    private HistoryService historyService;

    @Autowired
    public void setHistoryService(HistoryService historyService) {
        this.historyService = historyService;
    }

    @GetMapping("/get_anime_history")
    public JsonResult<AnimeHistoryParam> getAnimeHistory(HttpServletRequest request, Integer animeId) {
        String jwt = request.getHeader("Authorization");
        JwtUtils jwtUtils = new JwtUtils();
        jwtUtils.parseJwt(jwt);
        Long userId = jwtUtils.getUser().getId();
        AnimeHistoryParam animeHistoryParam = historyService.getHistoryByAnimeId(userId, animeId);
        return new JsonResult<>(200, animeHistoryParam, "success");
    }

    @GetMapping("/get_video_history")
    public JsonResult<AnimeHistoryParam> getVideoHistory(HttpServletRequest request, Long animeVideoId) {
        String jwt = request.getHeader("Authorization");
        JwtUtils jwtUtils = new JwtUtils();
        jwtUtils.parseJwt(jwt);
        Long userId = jwtUtils.getUser().getId();
        AnimeHistoryParam animeHistoryParam = historyService.getHistoryByAnimeVideoId(userId, animeVideoId);
        return new JsonResult<>(200, animeHistoryParam, "success");
    }

    @PostMapping("/set_history")
    public JsonResult<Void> setHistory(HttpServletRequest request, @Validated AnimeHistoryParam param) {
        String jwt = request.getHeader("Authorization");
        JwtUtils jwtUtils = new JwtUtils();
        jwtUtils.parseJwt(jwt);
        Long userId = jwtUtils.getUser().getId();
        historyService.setHistory(userId, param);
        return new JsonResult<>(200, null, "上传历史记录成功");
    }
}
